package com.example.timeplanning_teacher;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONObject;

public class BindStudentActivity extends AppCompatActivity {
private Button btn_bind;
private EditText et_StudentUsername;
private EditText et_StudentPsd;
private String StudentUsername;
private String StudentPassword;
private Toast timeOut_Toast;
private Toast noCount_Toast;
private Toast wrongPsd_Toast;
private Toast successBind_Toast;
private Toast failureBind_Toast;
private SharedPreferences sp;
private String parentUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_student);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        btn_bind = findViewById(R.id.bindStudent);
        et_StudentUsername = findViewById(R.id.et_StudentUsrname);
        et_StudentPsd = findViewById(R.id.et_StudentPsd);
        timeOut_Toast = Toast.makeText(BindStudentActivity.this,"网络连接超时",Toast.LENGTH_SHORT);
        noCount_Toast = Toast.makeText(BindStudentActivity.this,"该学生账号不存在",Toast.LENGTH_SHORT);
        wrongPsd_Toast = Toast.makeText(BindStudentActivity.this,"密码输入错误",Toast.LENGTH_SHORT);
        successBind_Toast = Toast.makeText(BindStudentActivity.this,"绑定成功",Toast.LENGTH_SHORT);
        failureBind_Toast = Toast.makeText(BindStudentActivity.this,"绑定失败",Toast.LENGTH_SHORT);
        sp = getSharedPreferences("parent", Context.MODE_PRIVATE);
        parentUsername = sp.getString("username","");

        btn_bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StudentUsername = et_StudentUsername.getText().toString();
                StudentPassword = et_StudentPsd.getText().toString();
                CheckStudent checkStudent = new CheckStudent();
                checkStudent.start();
            }
        });
    }
    public class CheckStudent extends Thread{

        @Override
        public void run() {
            try {
                String json = "{\n" +
                        "  \"username\": \"" + StudentUsername + "\",\n" +
                        "  \"password\": \"" + StudentPassword + "\"\n" +
                        "}";
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("http://58.58.36.198:9900/student/login")
                        .post(RequestBody.create(MediaType.parse("application/json"),json))
                        .build();
                Response response = client.newCall(request).execute();
                String responseData = response.body().string();
                JSONObject jsonObject=new JSONObject(responseData);
                int code1=(int)jsonObject.get("code");
                if(code1 == 100){
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("StudentUsername",StudentUsername);
                    editor.commit();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                String json = "{\n" +
                                        "  \"username\": \"" + parentUsername + "\",\n" +
                                        "  \"student_username\": \"" + StudentUsername + "\"\n" +
                                        "}";
                                OkHttpClient client = new OkHttpClient();
                                Request request = new Request.Builder()
                                        .url("http://58.58.36.198:9900/parent/bind")
                                        .post(RequestBody.create(MediaType.parse("application/json"),json))
                                        .build();
                                Response response = client.newCall(request).execute();
                                String responseData = response.body().string();
                                JSONObject jsonObject=new JSONObject(responseData);
                                int code2=(int)jsonObject.get("code");
                                if(code2 == 100){
                                    successBind_Toast.show();
                                    Intent intent = new Intent(BindStudentActivity.this,FragmentActivity.class);
                                    startActivity(intent);
                                    BindStudentActivity.this.finish();
                                }else{
                                    failureBind_Toast.show();
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                                timeOut_Toast.show();
                            }
                        }
                    }).start();
                }
                else if(code1 == 1){
                    noCount_Toast.show();
                }
                else{
                    wrongPsd_Toast.show();
                }
            }catch (Exception e){
                e.printStackTrace();
                timeOut_Toast.show();
            }
        }
    }
}